package com.animatedpicture.object;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Line2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.ImageIcon;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

public class Drum implements IMovingObject{
	
	private Point Pos;
	
	private final int w = 30,h = 20;
	private final int d_pos = 20;
	private final int one_X = -15,two_X = -10, three_X = 20;
	private final int one_Y = 10,two_Y = 20,three_Y = 10;
	private boolean ani_Flag = false;
	private int ch = 0;

	private AudioStream as;
	
	private String headType;
	private ImageIcon headImg;
	
	public Drum(String headType){
		Pos = null;
		
		// Get audio file from local storage
		try {
			File file = new File("./sound/Drum_Sound.wav");
			FileInputStream fis = new FileInputStream(file);
			as = new AudioStream(fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		this.headType = headType;
		
		switch (this.headType) {
		case "Default":
			headImg = null;
			break;
		case "Yap Che Een":
			headImg = new ImageIcon("./Image/Yap.png");
			break;
		case "Kim Ik Soo":
			headImg = new ImageIcon("./Image/Kim.png");
			break;
		case "Wong Kok Sheng":
			headImg = new ImageIcon("./Image/Wong.png");
			break;
		case "Shin Yong Tae":
			headImg = new ImageIcon("./Image/Shin.png");
			break;
		case "Kim Seock Yoon":
			headImg = new ImageIcon("./Image/Kim_S.png");
			break;
		default:
			break;
		}
	}
	
	@Override
	public void draw(Graphics2D g2) {
		
		if (Pos != null) {	
			Ellipse2D.Double Fir = new Ellipse2D.Double(Pos.x, Pos.y, w, h);
			Ellipse2D.Double Fir_b = new Ellipse2D.Double(Pos.x, Pos.y+h*2, w, h);
			Rectangle2D.Double Fir_r = new Rectangle2D.Double( Pos.x , Pos.y + h/2, w, h*2 );
			Line2D.Double foot1_1 = new Line2D.Double(Pos.x + w/2,Pos.y + h*2 + h - h/2, Pos.x + one_X + w/2,Pos.y + one_Y + h*2 + h - h/2 );
			Line2D.Double foot1_2 = new Line2D.Double(Pos.x + w/2,Pos.y + h*2 + h - h/2, Pos.x + two_X + w/2,Pos.y + two_Y + h*2 + h - h/2);
			Line2D.Double foot1_3 = new Line2D.Double(Pos.x + w/2,Pos.y + h*2 + h - h/2, Pos.x + three_X + w/2,Pos.y + three_Y + h*2 + h - h/2);
			
			Ellipse2D.Double Sec = new Ellipse2D.Double(Pos.x + d_pos * 2 - 5, Pos.y + d_pos, w+10, h);
			Ellipse2D.Double Sec_b = new Ellipse2D.Double(Pos.x + d_pos * 2 - 5, Pos.y + d_pos + h*2, w+10, h);
			Rectangle2D.Double Sec_r = new Rectangle2D.Double(Pos.x + d_pos * 2 - 5, Pos.y + d_pos + h/2, w+10, h*2 );
			
			Ellipse2D.Double Thr = new Ellipse2D.Double(Pos.x + d_pos * 4 - 5 ,Pos.y + d_pos, w+10, h);
			Ellipse2D.Double Thr_b = new Ellipse2D.Double(Pos.x + d_pos * 4 - 5 ,Pos.y + d_pos + h*2, w+10, h);
			Rectangle2D.Double Thr_r = new Rectangle2D.Double( Pos.x + d_pos * 4 - 5 ,Pos.y + d_pos + h/2, w+10, h*2 );

			Ellipse2D.Double For = new Ellipse2D.Double(Pos.x + d_pos * 6,Pos.y, w, h);
			Ellipse2D.Double For_b = new Ellipse2D.Double(Pos.x + d_pos * 6,Pos.y + h*2, w, h);
			Rectangle2D.Double For_r = new Rectangle2D.Double( Pos.x + d_pos * 6,Pos.y + h/2, w, h*2 );
			Line2D.Double foot4_1 = new Line2D.Double(Pos.x + d_pos * 6 + w/2,Pos.y + h*2 + h - h/2, Pos.x + d_pos * 6 + one_X + w/2,Pos.y + one_Y + h*2 + h - h/2 );
			Line2D.Double foot4_2 = new Line2D.Double(Pos.x + d_pos * 6 + w/2,Pos.y + h*2 + h - h/2, Pos.x + d_pos * 6 + two_X + w/2,Pos.y + two_Y + h*2 + h - h/2);
			Line2D.Double foot4_3 = new Line2D.Double(Pos.x + d_pos * 6 + w/2,Pos.y + h*2 + h - h/2, Pos.x + d_pos * 6 + three_X + w/2,Pos.y + three_Y + h*2 + h - h/2);

			Ellipse2D.Double five = new Ellipse2D.Double(Pos.x + d_pos * 8 - 20, Pos.y-20, w*2, h);
			Line2D.Double five_l = new Line2D.Double(Pos.x + d_pos*8 + 10, Pos.y, Pos.x + d_pos*8 + 10, Pos.y + 40);
			Line2D.Double foot5_1 = new Line2D.Double(Pos.x + d_pos*8 + 10, Pos.y + 40, Pos.x + d_pos*8 + 10 + one_X, Pos.y + 40 + one_Y);
			Line2D.Double foot5_2 = new Line2D.Double(Pos.x + d_pos*8 + 10, Pos.y + 40, Pos.x + d_pos*8 + 10 + two_X, Pos.y + 40 + two_Y);
			Line2D.Double foot5_3 = new Line2D.Double(Pos.x + d_pos*8 + 10, Pos.y + 40, Pos.x + d_pos*8 + 10 + three_X, Pos.y + 40 + three_Y);
			
			// chair
			Ellipse2D.Double chair = new Ellipse2D.Double(Pos.x + 60, Pos.y, w, h);
			Line2D.Double chair_l = new Line2D.Double(Pos.x + 60 + w/2, Pos.y + h, Pos.x + 60 + w/2, Pos.y+h+10);
			
			Graphics g = (Graphics)g2;
			
			// human
			Ellipse2D.Double head = new Ellipse2D.Double(Pos.x +65, Pos.y-40, w-10, h);
			
	//		if (headImg != null) {
		//		g.drawImage(headImg.getImage(),Pos.x +65, Pos.y-40 + 10, w-10 + 10, h + 10, null);
		//	}
			
			Line2D.Double body = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-20, Pos.x + 60 + w/2, Pos.y+h-10);
			Line2D.Double leg_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10, Pos.x + 60 + w/2-10, Pos.y+h-10 + 10);
			Line2D.Double leg_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10, Pos.x + 60 + w/2+10, Pos.y+h-10 + 10);
			Line2D.Double arm_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 - 20, Pos.y+h-10);
			Line2D.Double arm_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 + 20, Pos.y+h-10);
			Line2D.Double bar_L = new Line2D.Double(Pos.x + 60 + w/2 - 20, Pos.y+h-10, Pos.x + 60 + w/2 - 40, Pos.y+h-30);
			Line2D.Double bar_R = new Line2D.Double(Pos.x + 60 + w/2 + 20, Pos.y+h-10, Pos.x + 60 + w/2 + 40, Pos.y+h-30);

			if( ani_Flag == true ){
				if( ch == 0 ){
					++ ch;
					head = new Ellipse2D.Double(Pos.x +65, Pos.y-40, w-10, h);
					
					if (headImg != null) {
						g.drawImage(headImg.getImage(),Pos.x +65 - 10, Pos.y-40 - 15, w-10 + 20, h + 15, null);
					}
					
					body = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-20, Pos.x + 60 + w/2, Pos.y+h-10);
					leg_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10, Pos.x + 60 + w/2-10, Pos.y+h-10 + 10);
					leg_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10, Pos.x + 60 + w/2+10, Pos.y+h-10 + 10);
					arm_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 - 20, Pos.y+h-10);
					arm_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 + 20, Pos.y+h-10);
					bar_L = new Line2D.Double(Pos.x + 60 + w/2 - 20, Pos.y+h-10, Pos.x + 60 + w/2 - 40, Pos.y+h-30);
					bar_R = new Line2D.Double(Pos.x + 60 + w/2 + 20, Pos.y+h-10, Pos.x + 60 + w/2 + 40, Pos.y+h-30);
				}
				else if( ch == 1 ){
					++ ch;
					head = new Ellipse2D.Double(Pos.x +65, Pos.y-40 + 10, w-10 + 10, h + 10);
					
					if (headImg != null) {
						g.drawImage(headImg.getImage(),Pos.x +65 - 10, Pos.y-40 - 10, w-10 + 20, h + 15, null);
					}
					
					body = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2, Pos.y+h-10 + 5);
					leg_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10 + 5, Pos.x + 60 + w/2-10, Pos.y+h-10 + 10);
					leg_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10 + 5, Pos.x + 60 + w/2+10, Pos.y+h-10 + 10);
					arm_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 - 30, Pos.y - 20);
					arm_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 + 35, Pos.y );
					bar_L = new Line2D.Double(Pos.x + 60 + w/2 - 30, Pos.y - 20, Pos.x + 60 + w/2 - 40, Pos.y-50);
					bar_R = new Line2D.Double(Pos.x + 60 + w/2 + 35, Pos.y, Pos.x + 60 + w/2 + 25, Pos.y + 30);
				}
				else if( ch == 2 ){
					++ ch;
					head = new Ellipse2D.Double(Pos.x +65, Pos.y-40, w-10, h);
					
					if (headImg != null) {
						g.drawImage(headImg.getImage(),Pos.x +65 - 10, Pos.y-40 - 15, w-10 + 20, h + 15, null);
					}
					
					body = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-20, Pos.x + 60 + w/2, Pos.y+h-10);
					leg_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10, Pos.x + 60 + w/2-10, Pos.y+h-10 + 10);
					leg_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10, Pos.x + 60 + w/2+10, Pos.y+h-10 + 10);
					arm_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 - 25, Pos.y + h);
					arm_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 + 30, Pos.y - 20);
					bar_L = new Line2D.Double(Pos.x + 60 + w/2 - 25, Pos.y + h, Pos.x + 60 + w/2 - 50, Pos.y + 10 );
					bar_R = new Line2D.Double(Pos.x + 60 + w/2 + 30, Pos.y - 20, Pos.x + 60 + w/2 + 40, Pos.y-50);
				}
				else if( ch == 3 ){
					++ ch;
					head = new Ellipse2D.Double(Pos.x +65, Pos.y-40 + 10, w-10, h);
					
					if (headImg != null) {
						g.drawImage(headImg.getImage(),Pos.x +65 - 10, Pos.y-40 - 10, w-10 + 20, h + 15, null);
					}
					
					body = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2, Pos.y+h-10 + 5);
					leg_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10 + 5, Pos.x + 60 + w/2-10, Pos.y+h-10 + 10);
					leg_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10 + 5, Pos.x + 60 + w/2+10, Pos.y+h-10 + 10);
					arm_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 - 30, Pos.y - 20);
					arm_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 + 25, Pos.y + h);
					bar_L = new Line2D.Double(Pos.x + 60 + w/2 - 30, Pos.y - 20, Pos.x + 60 + w/2 - 40, Pos.y-50);
					bar_R = new Line2D.Double(Pos.x + 60 + w/2 + 25, Pos.y + h, Pos.x + 60 + w/2 + 50, Pos.y + 10 );
				}
				else if( ch == 4 ){
					++ ch;
					head = new Ellipse2D.Double(Pos.x +65, Pos.y-40, w-10, h);
					
					if (headImg != null) {
						g.drawImage(headImg.getImage(),Pos.x +65 - 10, Pos.y-40 - 15, w-10 + 20, h + 15, null);
					}
					
					body = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-20, Pos.x + 60 + w/2, Pos.y+h-10);
					leg_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10, Pos.x + 60 + w/2-10, Pos.y+h-10 + 10);
					leg_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10, Pos.x + 60 + w/2+10, Pos.y+h-10 + 10);
					arm_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 - 30, Pos.y + 5 );
					arm_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 + 30, Pos.y - 20);
					bar_L = new Line2D.Double(Pos.x + 60 + w/2 - 30, Pos.y + 5 , Pos.x + 60 + w/2 - 25, Pos.y + 30);
					bar_R = new Line2D.Double(Pos.x + 60 + w/2 + 30, Pos.y - 20, Pos.x + 60 + w/2 + 40, Pos.y-50);
				}
				else if( ch == 5 ){
					++ ch;
					head = new Ellipse2D.Double(Pos.x +65, Pos.y-40 + 10, w-10, h);
					
					if (headImg != null) {
						g.drawImage(headImg.getImage(),Pos.x +65 - 10, Pos.y-40 - 10, w-10 + 20, h + 15, null);
					}
					
					body = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2, Pos.y+h-10 + 5);
					leg_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10 + 5, Pos.x + 60 + w/2-10, Pos.y+h-10 + 10);
					leg_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10 + 5, Pos.x + 60 + w/2+10, Pos.y+h-10 + 10);
					arm_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 - 30, Pos.y - 20);
					arm_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 + 25, Pos.y + h);
					bar_L = new Line2D.Double(Pos.x + 60 + w/2 - 30, Pos.y - 20, Pos.x + 60 + w/2 - 40, Pos.y-50);
					bar_R = new Line2D.Double(Pos.x + 60 + w/2 + 25, Pos.y + h, Pos.x + 60 + w/2 + 50, Pos.y + 10 );
				}
				else if( ch == 6 ){
					++ ch;
					head = new Ellipse2D.Double(Pos.x +65, Pos.y-40, w-10, h);
					
					if (headImg != null) {
						g.drawImage(headImg.getImage(),Pos.x +65 - 10, Pos.y-40 - 15, w-10 + 20, h + 15, null);
					}
					
					body = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-20, Pos.x + 60 + w/2, Pos.y+h-10);
					leg_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10, Pos.x + 60 + w/2-10, Pos.y+h-10 + 10);
					leg_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10, Pos.x + 60 + w/2+10, Pos.y+h-10 + 10);
					arm_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 - 25, Pos.y + h);
					arm_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 + 30, Pos.y - 20);
					bar_L = new Line2D.Double(Pos.x + 60 + w/2 - 25, Pos.y + h, Pos.x + 60 + w/2 - 50, Pos.y + 10 );
					bar_R = new Line2D.Double(Pos.x + 60 + w/2 + 30, Pos.y - 20, Pos.x + 60 + w/2 + 40, Pos.y-50);
				}
				else if( ch == 7 ){
					ch = 0;
					head = new Ellipse2D.Double(Pos.x +65, Pos.y-40 + 10, w-10, h);
					
					if (headImg != null) {
						g.drawImage(headImg.getImage(),Pos.x +65 - 10, Pos.y-40 - 10, w-10 + 20, h + 15, null);
					}
					
					body = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2, Pos.y+h-10 + 5);
					leg_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10 + 5, Pos.x + 60 + w/2-10, Pos.y+h-10 + 10);
					leg_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10 + 5, Pos.x + 60 + w/2+10, Pos.y+h-10 + 10);
					arm_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 - 30, Pos.y - 20);
					arm_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 + 45, Pos.y + h - 30 );
					bar_L = new Line2D.Double(Pos.x + 60 + w/2 - 30, Pos.y - 20, Pos.x + 60 + w/2 - 40, Pos.y-50);
					bar_R = new Line2D.Double(Pos.x + 60 + w/2 + 45, Pos.y + h - 30, Pos.x + 60 + w/2 + 80, Pos.y - 15 );
				}
			}
			else{
				head = new Ellipse2D.Double(Pos.x +65, Pos.y-40, w-10, h);
				
				if (headImg != null) {
					g.drawImage(headImg.getImage(),Pos.x +65 - 10, Pos.y-40 - 15, w-10 + 20, h + 15, null);
				}
				
				body = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-20, Pos.x + 60 + w/2, Pos.y+h-10);
				leg_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10, Pos.x + 60 + w/2-10, Pos.y+h-10 + 10);
				leg_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y+h-10, Pos.x + 60 + w/2+10, Pos.y+h-10 + 10);
				arm_L = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 - 20, Pos.y+h-10);
				arm_R = new Line2D.Double(Pos.x + 60 + w/2, Pos.y-15, Pos.x + 60 + w/2 + 20, Pos.y+h-10);
				bar_L = new Line2D.Double(Pos.x + 60 + w/2 - 20, Pos.y+h-10, Pos.x + 60 + w/2 - 40, Pos.y+h-30);
				bar_R = new Line2D.Double(Pos.x + 60 + w/2 + 20, Pos.y+h-10, Pos.x + 60 + w/2 + 40, Pos.y+h-30);
			}

			g2.setStroke( new BasicStroke( 3 ) );
			g2.setColor( Color.magenta );
			g2.fill( Fir_b );
			g2.fill( Fir_r );
			g2.fill( Sec_b );
			g2.fill( Sec_r );
			g2.fill( Thr_b );
			g2.fill( Thr_r );
			g2.fill( For_b );
			g2.fill( For_r );
			
			g2.setColor( Color.black );
			g2.fill( Fir );
			g2.fill( Sec );
			g2.fill( Thr );
			g2.fill( For );
			g2.fill( five );
			g2.fill( chair );
			
			g2.setColor( Color.blue );
			if( headImg == null ){
				g2.fill(head);
			}
			g2.fill( body );
			
			g2.setColor( Color.magenta );

			g2.draw(Fir_r);
			g2.draw(Fir_b);
			g2.draw(Fir);
			
			g2.draw(Sec_r);
			g2.draw(Sec_b);
			g2.draw(Sec);
			
			g2.draw(Thr_r);
			g2.draw(Thr_b);
			g2.draw(Thr);
			
			g2.draw(For_r);
			g2.draw(For_b);
			g2.draw(For);
			
			g2.draw(five);
			g2.draw(five_l);
			
			g2.setColor( Color.black );
			g2.fill( Fir );
			g2.fill( Sec );
			g2.fill( Thr );
			g2.fill( For );
			g2.fill( five );
			g2.fill( chair );
			
			g2.setColor( Color.magenta );

			g2.draw(foot1_1);
			g2.draw(foot1_2);
			g2.draw(foot1_3);
			
			g2.draw(foot4_1);
			g2.draw(foot4_2);
			g2.draw(foot4_3);
			
			g2.draw(foot5_1);
			g2.draw(foot5_2);
			g2.draw(foot5_3);
			
			g2.draw(chair);
			g2.draw(chair_l);

			g2.setColor( Color.blue );
			
			if( headImg == null ){
				g2.draw(head);
			}
			g2.draw(body);
			g2.draw(leg_L);
			g2.draw(leg_R);
			g2.draw(arm_L);
			g2.draw(arm_R);
			
			g2.setColor( Color.green );
			
			g2.draw(bar_L);
			g2.draw(bar_R);
		}
	}

	@Override
	public void setPosition(Point mPoint) {
		Pos = mPoint;
		System.out.println("ObjectCar SetPosition : ("+mPoint.x+","+mPoint.y+")");
	}
	
	public void animate(boolean trriger){
		ani_Flag = trriger;
	}
	
	@Override
	public void playMusic(boolean trriger) {
		if (trriger) {
			AudioPlayer.player.start(as);
		}else {
			AudioPlayer.player.stop(as);
		}
	}
}
